﻿using Comm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Ztest
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            DateTime time1 = dateTimePicker1.Value;
            DateTime time2 = dateTimePicker2.Value;
            time1 = new DateTime(time1.Year, time1.Month, time1.Day);
            time2 = new DateTime(time2.Year, time2.Month, time2.Day);
            if (time1>= time2)
            {
                button1.Enabled = true;
                MessageBox.Show("开始时间必须小于结束时间(The start time must be before the end time)");
                return;
            }



             SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel 工作簿 (*.xls)|*.xls";
            saveFileDialog.Title = "导出到 Excel 文件";
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;

                if (!backgroundWorker1.IsBusy)
                {
                    label3.Text = "数据正在导出中(Exporting data)....";
                    backgroundWorker1.RunWorkerAsync(filePath);
                }
                else
                {
                    button1.Enabled = true;
                    MessageBox.Show("数据正在导出中，请稍后(Data is being exported, please wait)....");
                }
             
             
                //excel.ExportExcelFile()

            }
          
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now;
            dateTimePicker2.Value = DateTime.Now.AddDays(1);
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DateTime time1 = dateTimePicker1.Value;
            DateTime time2 = dateTimePicker2.Value;
            time1 = new DateTime(time1.Year, time1.Month, time1.Day);
            time2 = new DateTime(time2.Year, time2.Month, time2.Day);
            try
            {
                DataTable table = SqlDAL.GetHospitalList(time1, time2);
                string filePath = e.Argument.ToString();
                if (table != null && table.Rows.Count > 0)
                {
                    ExcelOperator excel = new ExcelOperator();
                    table.Columns[0].ColumnName = "扫码内容(Code)";
                    table.Columns[1].ColumnName = "扫码时间(Time)";
                    excel.ExportExcelFile(table, filePath);
                    e.Result = "导出完成(Succeed)";
                }
                else
                {
                    e.Result = "没有要导出的数据(No Data)";
                }
            }
            catch (Exception ex)
            {

                e.Result = "导出失败(Fail)！" + ex.Message;
            }
       
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dateTimePicker1.Enabled = true;
            dateTimePicker2.Enabled = true;
            button1.Enabled = true;
            this.label3.Text=e.Result.ToString();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
